﻿using System;
using System.Windows.Forms;
using EntityBLL;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            CtrAdmin admin = new CtrAdmin();
            string cdKey = admin.GetConfig().CDKey;
            Register reg = new Register();
            Program.IsActive = reg.CheckCdKey(cdKey);
            if (!Program.IsActive)
                this.Text = this.Text + " (Trial version)";
            foreach (ToolStripMenuItem item in menuStrip1.Items)
            {
                foreach (ToolStripMenuItem item1 in item.DropDownItems)
                {
                    if (CtrAdmin.GetPermission(Program.UserEntity.Permission, item1.Name) == 0)
                    {
                        item1.Enabled = false;
                    }
                }
            }
            if (Program.UserEntity.m_userName == "admin")
                mnuManDatabase.Visible = true;
            else
                mnuManDatabase.Visible = false;
        }

        private frmTableService frmT1 = null;
        private void frmMain_Load(object sender, EventArgs e)
        {
            if (Program.isLogin == false)
            {
                Program.frmLoginMain.ShowDialog();
            }
            if (frmT1 == null)
            {
                frmT1 = new frmTableService();
                frmT1.MdiParent = this;
                frmT1.FormClosed += delegate { frmT1 = null; };
                frmT1.Show();
            }
            else
                frmT1.Select();
        }
        private void mnuTableService_Click(object sender, EventArgs e)
        {
            if (frmT1 == null)
            {
                frmT1 = new frmTableService();
                frmT1.MdiParent = this;
                frmT1.FormClosed += delegate { frmT1 = null; };
                frmT1.Show();
            }
            else
                frmT1.Select();
        }
        
        private void mnuManCategory_Click(object sender, EventArgs e)
        {
            frmCategory frmCategory = new frmCategory();
            frmCategory.ShowDialog();            
        }


        private void mnuRange_Click(object sender, EventArgs e)
        {
            frmRange frmRange = new frmRange();
            frmRange.ShowDialog();
        }
        private void mnuManTable_Click(object sender, EventArgs e)
        {
            frmTable frmTable = new frmTable();
            frmTable.ShowDialog();
        }

        private frmProduct frmProduct = null;
        private void mnuManProduct_Click(object sender, EventArgs e)
        {
            if (frmProduct == null)
            {
                frmProduct = new frmProduct();
                frmProduct.MdiParent = this;
                frmProduct.FormClosed += delegate { frmProduct = null; };
                frmProduct.Show();
            }
            else
                frmProduct.Select();
        }

        private void mnuChangePass_Click(object sender, EventArgs e)
        {
            frmChangePass frmChangePass = new frmChangePass();
            frmChangePass.ShowDialog();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuLogOff_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
            Program.isLogin = false;
            frmLogin frmLogIn = new frmLogin();
            frmLogIn.ShowDialog();
        }

        private void mnuRegistryUser_Click(object sender, EventArgs e)
        {
            frmUser frmUser = new frmUser();
            frmUser.ShowDialog();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có đồng ý thoát hệ thống không?", "Thông báo...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                e.Cancel = true;
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void mnuConnectDB_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
            frmConfigDB frmConfigDB = new frmConfigDB();
            if (frmConfigDB.ShowDialog() == DialogResult.OK)
            {
                Program.isLogin = false;
                frmMain_Load(null, null);
            }
        }

        private void mnuBackupDB_Click(object sender, EventArgs e)
        {
            if (!Program.IsActive)
            {
                MessageBox.Show("Bạn cần đăng ký sử dụng phần mềm để sử dụng chức năng này!", "Thông báo");
                return;
            }
            frmBackupDB frmBackupDB = new frmBackupDB();
            frmBackupDB.ShowDialog();
        }

        private void mnuCreateNewDB_Click(object sender, EventArgs e)
        {
            frmNewDB frmNewDB = new frmNewDB();
            frmNewDB.ShowDialog();
        }

        private frmQuantification frmQuantifi = null;
        private void mnuQuantification_Click(object sender, EventArgs e)
        {            
            if (frmQuantifi == null)
            {
                frmQuantifi = new frmQuantification();
                frmQuantifi.MdiParent = this;
                frmQuantifi.FormClosed += delegate { frmQuantifi = null; };
                frmQuantifi.Show();
            }
            else
                frmQuantifi.Select();
        }

        private void mnuManUnit_Click(object sender, EventArgs e)
        {
            frmUnit frmUnit = new frmUnit();
            frmUnit.ShowInTaskbar = false;
            //frmUnit.ShowIcon = false;
            frmUnit.ShowDialog();
        }

        private frmStock frmStock = null;
        private void mnuImportStock_Click(object sender, EventArgs e)
        {
            if (frmStock == null)
            {
                frmStock = new frmStock();
                frmStock.MdiParent = this;
                frmStock.FormClosed += delegate { frmStock = null; };
                frmStock.Show();
            }
            else
                frmStock.Select();
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QLCF.Forms.admin.frmAdmin admin = new admin.frmAdmin();
            admin.Show();
        }

        private void mnuListAdmin_Click(object sender, EventArgs e)
        {
            admin.frmListAdmin frmadmin = new admin.frmListAdmin();
            frmadmin.ShowDialog();
        }

        private void mnuPermission_Click(object sender, EventArgs e)
        {
            QLCF.Forms.admin.frmAdmin frmPer = new admin.frmAdmin();
            frmPer.ShowDialog();
        }

        private void mnuRole_Click(object sender, EventArgs e)
        {
            QLCF.Forms.admin.frmRole frmRol = new admin.frmRole();
            frmRol.ShowDialog();
        }

        private Reports.frmBillReport frmBillReport = null;
        private void mnuReportSale_Click(object sender, EventArgs e)
        {            
            if (frmBillReport == null)
            {
                frmBillReport = new Reports.frmBillReport();
                frmBillReport.MdiParent = this;
                frmBillReport.FormClosed += delegate { frmBillReport = null; };
                frmBillReport.Show();
            }
            else
                frmBillReport.Select();
        }

        private Reports.frmStockReport frmStockReport = null;
        private void mnuReportStock_Click(object sender, EventArgs e)
        {            
            frmStockReport = new Reports.frmStockReport();            
            frmStockReport.ShowDialog();            
        }
        
        private void mnuLog_Click(object sender, EventArgs e)
        {            

            QLCF.Forms.admin.frmLog frmlog = new QLCF.Forms.admin.frmLog();

            frmlog.ShowDialog();
        }

        private void mnuRegister_Click(object sender, EventArgs e)
        {
            frmKeyCode code = new frmKeyCode();
            code.ShowDialog();
        }

        private void mnuConfig_Click(object sender, EventArgs e)
        {
            if (!Program.IsActive)
            {
                MessageBox.Show("Bạn cần đăng ký sử dụng phần mềm để sử dụng chức năng này!", "Thông báo");
                return;
            }
            frmConfig frmC = new frmConfig();
            frmC.ShowDialog();
        }

        private void mnuContact_Click(object sender, EventArgs e)
        {
            AboutBoxAuthor box = new AboutBoxAuthor();
            box.ShowDialog();
        }

        private void mnuRestoreDataBase_Click(object sender, EventArgs e)
        {
            if (!Program.IsActive)
            {
                MessageBox.Show("Bạn cần đăng ký sử dụng phần mềm để sử dụng chức năng này!", "Thông báo");
                return;
            }
            frmRestoreDB frms = new frmRestoreDB();
            frms.ShowDialog();
        }

        private void mnuNcc_Click(object sender, EventArgs e)
        {
            frmDistribute frms = new frmDistribute();
            frms.ShowDialog();
        }         
    }
}
